﻿import WebSocktInterface from "@/utils/webSocket/WebSocketHelper.js";

//音频接口对象
let VoiceInterfaceObject = {
    /*
    * 当前实体对象
    * 下行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "" }
    * 上行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "", "Message": "" }
    */
   
    GetSendModel: function (action, params) {
        var p_action = action || "";
        var p_params = params || "";

        let phoneMsg = JSON.parse(localStorage.getItem('phoneMsg'))
        
        let SystemAttr = {
            CurrentUser: { "DepartmentID": "Default", "GongHao": '', "FenJi": '', "AgentGroupName": "", "Params": "" }
        }
        return { "Action": p_action, "GongHao": phoneMsg.UserJobNumber, "FenJi": phoneMsg.UserExtensionNumber, "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": p_params };
    },
    //弹屏 回调
    Back_TelPhoneEvent: function (returnVal) {
        //Tel,GongHao,URL,Area,Guid,IVR,Remark
        var DialArray = returnVal.Message.split(",");
        var DialInfo = {};
        DialInfo.telNum = DialArray[0]; //来电号码
        DialInfo.telGongHao = DialArray[1]; //来电工号
        DialInfo.telArea = DialArray[3]; //来电区域
        DialInfo.telGuid = DialArray[4]; //来电GUID
        DialInfo.telIVR = DialArray[5]; //来电IVR名称
        DialInfo.telType = returnVal.Params; //来电 外呼 转接

        // layer.open({
        //     title: "来电弹屏",
        //     type: 1,
        //     area: ['600px', '360px'],
        //     shadeClose: true, //点击遮罩关闭
        //     content: '\<\div style="padding:10px;">来电号码：' + DialInfo.telNum + '<br>来电工号：' + DialInfo.telGongHao + '<br>来电区域：' + DialInfo.telArea + '<br>来电GUID：' + DialInfo.telGuid + '<br>来电IVR名称：' + DialInfo.telIVR + '<br>来电类型：' + DialInfo.telType + ' \<\/div>'
        // });
        
    },
    //弹屏 回调
    Back_RegNumberState: function (returnVal) {
        //Tel,GongHao,URL,Area,Guid,IVR,Remark
    },
    //心跳 回调
    Back_keeplive: function (returnVal) {
        // console.log(returnVal);
    },
    //登录
    Login: function () {
        var modelJosn = this.GetSendModel("Login");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //登录回调
    Back_Login:function(returnVal) {
        
        if (returnVal.Params == "0") {
            WebSocktInterface("KeepAlive");
            alert("登录语音系统成功！");
        } else {
            alert("登录语音系统失败！");
        }
    },

    //设置用户权限
    SetGroupRole: function () {
        var modelJosn = this.GetSendModel("SetGroupRole", SystemAttr.CurrentUser.AgentGroupName);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },


    //设置用户权限 回调
    Back_SetGroupRole: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },

    // 设置录音权限
    SetRecRole: function (val) {
        var modelJosn = { "Action": "SetRecRole", "GongHao": SystemAttr.CurrentUser.GongHao, "FenJi": SystemAttr.CurrentUser.FenJi, "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": val };
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    // 设置录音权限回调
    Back_SetRecRole: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //登出 当前座席
    LogOut: function () {
        var modelJosn = this.GetSendModel("LogOut");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //强制登出
    F_LogOut: function (gongHao) {
        var modelJosn = { "Action": "LogOut", "GongHao": gongHao, "FenJi": "", "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": "" };
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //登出回调
    Back_LogOut: function (returnVal) {
        alert("登出系统成功");
    },
    //示忙当前座席
    SetBusy: function () {
        var modelJosn = this.GetSendModel("SetBusy");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //强制示忙座席
    F_SetBusy: function (gongHao) {
        var modelJosn = { "Action": "SetBusy", "GongHao": gongHao, "FenJi": "", "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": "" };
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //示忙回调
    Back_SetBusy: function (returnVal) {
        if (returnVal.Params == "0") {
            alert("示忙成功！");
        } else {
            alert(returnVal.Message);
        }
    },
    //示闲当前座席
    SetIdle: function () {
        var modelJosn = this.GetSendModel("SetIdle");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },

    Message: function () {
        var modelJosn = this.GetSendModel("Message");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //强制示闲座席
    F_SetIdle: function (gongHao) {
        var modelJosn = { "Action": "SetIdle", "GongHao": gongHao, "FenJi": "", "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": "" };
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //示闲回调
    Back_SetIdle: function (returnVal) {
        if (returnVal.Params == "0") {
            alert("示闲成功！");
        } else {
            alert(returnVal.Message);
        }
    },
    //保持
    KeepInTouch: function () {
        var modelJosn = this.GetSendModel("KeepInTouch");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //保持回调
    Back_KeepInTouch: function (returnVal) {
        if (returnVal.Params == "0") {
            alert("保持成功！");
        } else {
            alert(returnVal.Message);
        }
    },
    //取消保持
    KeepCancelInTouch: function () {
        var modelJosn = this.GetSendModel("KeepCancelInTouch");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //取消保持回调
    Back_KeepCancelInTouch: function (returnVal) {
        if (returnVal.Params == "0") {
            alert("取消保持成功！");
        } else {
            alert(returnVal.Message);
        }
    },
    //开启事后
    TalkedDealBegin: function () {
        var modelJosn = this.GetSendModel("KeepCancelInTouch");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //开启事后 回调
    Back_TalkedDealBegin: function (returnVal) {

    },
    //结束事后
    TalkedDealEnd: function () {

    },
    //结束事后 回调
    Back_TalkedDealEnd: function (returnVal) {

    },
    //语音呼叫
    DialOut: function (phone) {
        var modelJosn = this.GetSendModel("DialOut", phone);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //视频呼叫
    VideoCall: function (phone) {
        var modelJosn = this.GetSendModel("VideoCall", phone);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //外呼回调
    Back_DialOut: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //挂断
    HangUp: function () {
        var modelJosn = this.GetSendModel("HangUp");
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },

    //强拆 挂断
    Rtmp: function (phone) {
        var modelJosn = this.GetSendModel("Rtmp", phone);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));

        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },

    //强拆 挂断
    startautodial: function (phone) {
        var modelJosn = this.GetSendModel("StartAutoDial", phone);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));

        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },

    //强拆 挂断
    Message: function (phone) {
        var modelJosn = this.GetSendModel("Notice", phone);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));

        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },

    //外呼回调
    Back_Rtmp: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },

    //强拆 挂断
    F_HangUp: function (gongHao) {
        var modelJosn = { "Action": "HangUp", "GongHao": gongHao, "FenJi": "", "PlatFormCode": SystemAttr.CurrentUser.DepartmentID, "Params": "" };
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //挂断 回调
    Back_HangUp: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //转接
    TeleSwitch: function (gongHao) {
        var modelJosn = this.GetSendModel("TeleSwitch", gongHao);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //转接 回调
    Back_TeleSwitch: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //代接
    InsteadOfTele: function (gonghao) {
        var modelJosn = this.GetSendModel("InsteadOfTele", gonghao);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //代接 回调
    Back_InsteadOfTele: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //强插
    CancleChannel: function (gongHao) {
        var modelJosn = this.GetSendModel("CancleChannel", gongHao);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //强插 回调
    Back_CancleChannel: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //监听
    LinkTele: function (gongHao) {
        var modelJosn = this.GetSendModel("LinkTele", gongHao);
        var retruenVal = WebSocktInterface("Send", JSON.stringify(modelJosn));
        if (retruenVal != 1) {
            alert(WebSocktInterface("GetError", retruenVal));
        }
    },
    //监听 回调
    Back_LinkTele: function (returnVal) {
        if (returnVal.Params == "0") {
            alert(returnVal.Message);
        } else {
            alert(returnVal.Message);
        }
    },
    //分机注册状态回调
    Back_RegNumberState: function (returnVal) {
        // console.log(returnVal);
    },
    //通话挂机事件
    Back_TalkingEnd: function (returnVal) {
        if (returnVal.Params == "0") {
            // alert(returnVal.Message);
            // 拿到通话挂机时间以及时间戳
            let stopTime = new Date();         //获取当前时间
            let stopTimer = Date.parse(new Date());        //获取当前时间戳
            let obj = {
                time:stopTimer,
                msg:returnVal.Message
            }
            return obj
            // console.log('结束通话时间',stopTime)
        } else {
            alert(returnVal.Message);
        }
    },
    //开始通话事件
    Back_BeginTalking: function (returnVal) {
        if (returnVal.Params == "0") {
            // alert(returnVal.Message);
            // 拿到开始通话时间以及时间戳
            let beginTime = new Date();         //获取当前时间
            let beginTimer = Date.parse(new Date());        //获取当前时间戳
            // console.log('开始通话时间',beginTimer)
        } else {
            // alert(returnVal.Message);
        }
    },
    Back_StartVideoMeeting: function (returnVal) {
        // console.log(returnVal);
    },
    Back_EndVideoTalking:function(returnVal){
        // if (returnVal.Params == "0") {
        //     alert(returnVal.Message);
        // } else {
        //     alert(returnVal.Message);
        // }
    },
    Back_TelQuene:function(returnVal){
        if (returnVal.Params == "0") {
            // alert(returnVal.Message);
            // 拿到开始通话时间以及时间戳
            let beginTime = new Date();         //获取当前时间
            let beginTimer = Date.parse(new Date());        //获取当前时间戳
            let obj = {
                tel:returnVal.Tel,
                time:beginTimer
            }
            return obj
        } else {
            // alert(returnVal.Message);
        }
    },


}

export default VoiceInterfaceObject
